package zp.weibo.util;

import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class NetUtil {
	
	public static final String TAG ="NET";

	public static InputStream Get(String url,String basicAuth){
		
		try{
			URL u = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) u.openConnection();
			conn.setRequestProperty("Authorization", basicAuth);
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(10*1000);
			int rsp = conn.getResponseCode();
			if(rsp!=200){
				Log.e(TAG, "请求错误："+String.valueOf(rsp)+" URL:"+url+" AUTH:"+basicAuth );
				return null;
			}

			return conn.getInputStream();
		}catch (Exception e) {
			Log.e(TAG, "请求异常："+e.toString()+" URL:"+url+" AUTH:"+basicAuth);
		}
		
		return null;
	}
	
	public static InputStream Post(String url,String basicAuth,String param){
		try{
			URL u = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) u.openConnection();
			conn.setRequestProperty("Authorization", basicAuth);
			conn.setRequestMethod("POST");
			conn.setConnectTimeout(10*1000);
			conn.setDoOutput(true);
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
//			String str = String.format(Locale.CHINA, "id=%s&&comment=%s",msgid,comment);
			
			out.write(param);
			out.flush();
			out.close();
			int rsp = conn.getResponseCode();
			if(rsp!=200){
				Log.e(TAG, "请求错误："+String.valueOf(rsp)+" URL:"+url+" AUTH:"+basicAuth );
				return null;
			}

			return conn.getInputStream();
		}catch (Exception e) {
			Log.e(TAG, "请求异常："+e.toString()+" URL:"+url+" AUTH:"+basicAuth);
		}
		
		return null;
	}
}
